package com.ruoyi.project.business.salesDetail.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.salesDetail.domain.OutSalesDetail;
import com.ruoyi.project.business.salesDetail.domain.YlSalesDetail;
import com.ruoyi.project.business.salesDetail.service.IYlSalesDetailService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 销售流水Controller
 * 
 * @author ruoyi
 * @date 2021-04-27
 */
@Controller
@RequestMapping("/business/salesDetail")
public class YlSalesDetailController extends BaseController
{
    private String prefix = "business/salesDetail";

    @Autowired
    private IYlSalesDetailService ylSalesDetailService;

    @RequiresPermissions("business:salesDetail:view")
    @GetMapping()
    public String salesDetail()
    {
        return prefix + "/salesDetail";
    }

    /**
     * 查询销售流水列表
     */
    @RequiresPermissions("business:salesDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YlSalesDetail ylSalesDetail)
    {
        startPage();
        List<YlSalesDetail> list = ylSalesDetailService.selectYlSalesDetailList(ylSalesDetail);
        return getDataTable(list);
    }

    /**
     * 导出销售流水列表
     */
    @RequiresPermissions("business:salesDetail:export")
    @Log(title = "销售流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YlSalesDetail ylSalesDetail)
    {
        List<YlSalesDetail> list = ylSalesDetailService.selectYlSalesDetailList(ylSalesDetail);
        ExcelUtil<YlSalesDetail> util = new ExcelUtil<YlSalesDetail>(YlSalesDetail.class);
        return util.exportExcel(list, "销售流水数据");
    }

    /**
     * 新增销售流水
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存销售流水
     */
    @RequiresPermissions("business:salesDetail:add")
    @Log(title = "销售流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YlSalesDetail ylSalesDetail)
    {
        return toAjax(ylSalesDetailService.insertYlSalesDetail(ylSalesDetail));
    }

    /**
     * 修改销售流水
     */
    @GetMapping("/edit/{salesId}")
    public String edit(@PathVariable("salesId") Long salesId, ModelMap mmap)
    {
        YlSalesDetail ylSalesDetail = ylSalesDetailService.selectYlSalesDetailById(salesId);
        mmap.put("ylSalesDetail", ylSalesDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售流水
     */
    @RequiresPermissions("business:salesDetail:edit")
    @Log(title = "销售流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YlSalesDetail ylSalesDetail)
    {
        return toAjax(ylSalesDetailService.updateYlSalesDetail(ylSalesDetail));
    }

    /**
     * 删除销售流水
     */
    @RequiresPermissions("business:salesDetail:remove")
    @Log(title = "销售流水", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ylSalesDetailService.deleteYlSalesDetailByIds(ids));
    }
    
    /**
     * 通知记录销售流水
     */
    @Log(title = "销售流水", businessType = BusinessType.INSERT)
    @PostMapping("/public/record")
    @ResponseBody
    public String record(OutSalesDetail outSalesDetail)
    {
    	YlSalesDetail ylSalesDetail = new YlSalesDetail();
    	ylSalesDetail.setMachineId(outSalesDetail.getMachid());
    	ylSalesDetail.setPickCode(outSalesDetail.getPickcode());
    	ylSalesDetail.setProductName(outSalesDetail.getName());
    	ylSalesDetail.setProductPrice(outSalesDetail.getPrice());
    	ylSalesDetail.setTrackNo(outSalesDetail.getTrackno());
    	ylSalesDetail.setSaleTime(new Date(outSalesDetail.getSaletime()));
        return ylSalesDetailService.recordYlSalesDetail(ylSalesDetail);
    }
}
